package aex.action;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.Article;
import aex.domain.ArticleCategory;
import aex.domain.MessageBoard;
import aex.domain.Operation;
import aex.domain.Puser;
import aex.domain.Score;
import aex.domain.UserMail;
import aex.service.ArticleCategoryService;
import aex.service.ArticleService;
import aex.service.CategoryEditerService;
import aex.service.CommentArticleService;
import aex.service.MedicinesService;
import aex.service.MessageBoardService;
import aex.service.OperationService;
import aex.service.PuserService;
import aex.service.ScoreService;
import aex.service.UserMailService;
import aex.util.CU;
import aex.util.Constant;
import aex.util.PageBean;
/**
 * 用户业务处理类
 * @author JiangYouYao
 *
 */
@Controller("UserAction")
@Scope("prototype")
public class UserAction extends BaseActon  {

	private static final long serialVersionUID = 1L;


	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;
	
	@Resource(name = CategoryEditerService.SERVICE_NAME)
	CategoryEditerService ceService;
	
	@Resource(name = MedicinesService.SERVICE_NAME)
	MedicinesService medicinesService;
	
	@Resource(name = PuserService.SERVICE_NAME)
	PuserService puserService;
	
	
	@Resource(name = ArticleService.SERVICE_NAME)
	ArticleService artService;

	@Resource(name = CommentArticleService.SERVICE_NAME)
	CommentArticleService comaService;
	
	@Resource(name = MessageBoardService.SERVICE_NAME)
	MessageBoardService mbService;
	
	@Resource(name = UserMailService.SERVICE_NAME)
	UserMailService  mailService;
	
	@Resource(name = ScoreService.SERVICE_NAME)
	ScoreService  scoreService;
	
	@Resource(name = OperationService.SERVICE_NAME)
	OperationService opService;

	public String deleteLy(){
		
		Puser user=(Puser) request.getSession().getAttribute("user");
		MessageBoard ly=mbService.findByID(request.getParameter("id"));
		Set<MessageBoard> set= user.getMessageBoardsForMbPued();
		for (MessageBoard messageBoard : set) {
			if(messageBoard.getMbId().equals(ly.getMbId())){
				set.remove(messageBoard);
				break;
			}
		}
		mbService.deleteById(ly.getMbId());
		return CU.goMsgPage(request, "留言删除成功！", "ok", "user/user_myHome.do");
		
	}
	

	public UserAction() {
		CU.deBugPrintln("UserAction 被创建");
	}

	
	/**
	 * 发布一篇文章
	 * @return
	 */
	public String userAddArticle(){
		Puser user=(Puser) request.getSession().getAttribute("user");
		String id=request.getParameter("acId");
		ArticleCategory cate=acService.findByID(id);
		Article art=new Article();
		art.setArTitle(request.getParameter("arTitle"));
		art.setArContent(request.getParameter("arContent"));
		art.setPuser(user);
		art.setArType(0);
		art.setArTime(CU.getSqlTime());
		art.setArticleCategory(cate);
		art.setArViewCount(0);
		art.setArLike(0);
		art.setArComentCount(0);
		art.setArAbstract(request.getParameter("arAbstract"));
		artService.save(art);
		
		changeScore(user,Constant.SCORE_FBWZ);
		
		return CU.goMsgPage(request, "主题发布成功，快去看看吧！", "ok", "visitor/visitor_article.do?arId="+art.getArId());
	}


	
	
	/**
	 * 更新文章内容
	 * @return
	 */
	public String userEditorArticle(){
		
		int arId =Integer.parseInt(request.getParameter("arId"));
		Article newArt=artService.findByID(arId);
		newArt.setArTitle(request.getParameter("arTitle"));
		newArt.setArContent(request.getParameter("arContent"));
		newArt.setArAbstract(request.getParameter("arAbstract"));
		artService.save(newArt);
		return CU.goMsgPage(request, "主题更新成功，快去看看吧！", "ok", "visitor/visitor_article.do?arId="+newArt.getArId());
		
	}
	
	/**
	 * 去文章编辑界面
	 * @return
	 */
	public String goUserEditorArticle(){
		Puser user=(Puser) request.getSession().getAttribute("user");
		Article art=artService.findByID( request.getParameter("id"));
		if(user.getPuId().equals(art.getPuser().getPuId())){
		String id=request.getParameter("id");
		request.setAttribute("article", art);
		return "userEditorArticle";
		}else{
			return CU.goMsgPage(request, "TMD", "err", "#");
		}
		
		
	}
	
	/**
	 * 删除一篇文章
	 * 
	 */
	public String deleteArticle(){
		Article art=artService.findByID( request.getParameter("id"));
		Puser user=(Puser) request.getSession().getAttribute("user");
		if(user.getPuId().equals(art.getPuser().getPuId())){
			artService.deleteById(art.getArId(),request.getContextPath());
			changeScore(user,Constant.SCORE_SCWZ);
			return CU.goMsgPage(request, "文章已经删除！", "ok", "visitor/visitor_showCategory.do?id="+art.getArticleCategory().getAcId());
		}else{
			return CU.goMsgPage(request, "TMD", "err", "#");
		}
	}
	
	
	
	/**
	 * 进入用户中心
	 * @return
	 */
	public String myHome(){
		int userId=((Puser)(request.getSession().getAttribute("user"))).getPuId();
		request.getSession().removeAttribute("user");
		Puser user=puserService.findById(userId);
		request.getSession().setAttribute("user", user);
		PageBean<Article> pageBean = new PageBean<Article>();
		PageBean.initPageBean(pageBean, request,"");
		request.setAttribute("pageBean", pageBean);
		pageBean = artService.findByUser(pageBean, userId);
		return "myHome";
	}
	
	
	/**
	 * 刷新聊天显示
	 * @return
	 */
	public String mails(){
		String herId=request.getParameter("herId");
		Puser user=(Puser) request.getSession().getAttribute("user");
		
		List<Puser> contacts=mailService.getContacts(user.getPuId());
		
		request.setAttribute("contacts", contacts);
		if(!herId.equals("init")){//带参数进入聊天界面	
			List<UserMail> mails=mailService.findMail(user.getPuId(), herId);
			request.setAttribute("mails", mails);
			request.setAttribute("herId", herId);
			return "mails";
		}else{
			request.setAttribute("herId", 0);
			return "mails";
		}
		
		
	}
	
	
	/**
	 * 发送消息
	 * @return
	 */
	public String herId=null;
	public String sendMail(){
		herId=request.getParameter("herId");
		Puser user=(Puser) request.getSession().getAttribute("user");
		Puser her=puserService.findById(Integer.parseInt(herId));
		UserMail mail=new UserMail();
		mail.setPuserByPuAcceput(her);
		mail.setPuserByPuSend(user);
		mail.setUmContent(request.getParameter("content"));
		mail.setUmTime(CU.getSqlTime());
		mailService.save(mail);
		return "sendMail";
		
		
	}
	
	
	
	
	
	/**
	 * 变更用户积分
	 * @param user
	 * @param operation
	 */
	public void changeScore(Puser user,String operation) {
		Score score=scoreService.getScoreByName(operation);
		user.setPuScore(user.getPuScore()+Integer.parseInt(score.getScValue()));
		Operation op=new Operation();
		op.setScore(score);
		op.setOpTime(CU.getSqlTime());
		op.setPuser(user);
		opService.save(op);
		puserService.update(user);
	}
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
